﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Common
{
    public class PathHelper
    {
        //private bool IsFile = false;
        //private string FileName = null;
        //private string LastFloder = null;
        //private string LastFloderPath = null;
        //public List<string> FlodersPath = new List<string>(10);
        //  			"F:\\Programming\\CSharp\\SFJDall\\SFJDManagement\\bin\\Debug\\SFJDService.pdb"
        public PathHelper(string path)
        {
            if (File.Exists(path))
            {
                this.IsFile = true;
                this.FullPath = path.Split('\\');
                this.FileName = this.FullPath[this.FullPath.Length - 1];
                this.LastFolderPath = path.Remove(path.Length - this.FileName.Length, this.FileName.Length);
                this.LastFolder = this.FullPath[this.FullPath.Length - 2];
            }
            else
            {
                IsFile = false;
                this.LastFolderPath = path[path.Length - 1].Equals('\\') ? path : path + "\\";
                this.FullPath = path.Split('\\');
                this.LastFolder = this.FullPath[this.FullPath.Length - 1];
            }
        }

        /// <summary>
        /// 给出的路径是否指向文件
        /// </summary>
        public bool IsFile { set; get; }

        /// <summary>
        /// 文件名
        /// </summary>
        public string FileName { set; get; }

        /// <summary>
        /// 最后一个文件夹的名称
        /// </summary>
        public string LastFolder { set; get; }

        /// <summary>
        /// 最后一个文件夹的全路径
        /// </summary>
        public string LastFolderPath { set; get; }

        /// <summary>
        /// 用List保存的全路径
        /// </summary>
        public string[] FullPath { set; get; }

        public static string GetLastFolderOfAll(string[] paths)
        {
            List<PathHelper> pats = new List<PathHelper>(paths.Length);
            foreach(string str in paths)
            {
                pats.Add(new PathHelper(str));
            }
            foreach(PathHelper pat in pats)
            {
                if (pat.IsFile)
                {
                    return pat.LastFolderPath;
                }
            }
            return paths.Length > 1 ? pats[0].LastFolderPath.Remove(pats[0].LastFolderPath.Length - 1 - pats[0].LastFolder.Length, pats[0].LastFolder.Length + 1) : pats[0].LastFolderPath;
        }

        public static string[] GetDirectory(string pathFolder)
        {
            string[] files = Directory.GetFileSystemEntries(pathFolder, "*", SearchOption.AllDirectories);
            return files;
        }

        /// <summary>
        /// 把输入的多个文件/文件夹 转换为List
        /// List[0]储存的是所有文件/文件夹的公共路径，List[n>0]储存的是List[0]下的相对路径
        /// </summary>
        /// <param name="paths"></param>
        /// <returns></returns>
        public static List<string> GetDirectory2List(string[] paths)
        {
            List<string> list = new List<string>(paths.Length + 1);
            string rootPath = GetLastFolderOfAll(paths);
            list.Add(rootPath);
            foreach(string path in paths)
            {
                if (!Directory.Exists(path))
                {
                    list.Add(path.Remove(0, rootPath.Length));
                }
                else
                {
                    string[] buff = Directory.GetFileSystemEntries(path, "*", SearchOption.AllDirectories);
                    foreach(string buf in buff)
                    {
                        PathHelper help = new PathHelper(buf);
                        if (help.IsFile)
                        {
                            if (!help.FileName.Equals("Thumbs.db"))
                            {
                                list.Add(buf.Remove(0, rootPath.Length));
                            }
                        }
                    }
                }
            }
            return list;
        }
    }
}
